package it.uniroma2.art.owlart.langsearch;

import it.uniroma2.art.owlart.langsearch.index.config.StandardAnalysisConfiguration;
import it.uniroma2.art.owlart.langsearch.search.OntIndexSearcher;
import it.uniroma2.art.owlart.model.ARTURIResource;
import it.uniroma2.art.owlart.protegeimpl.factory.ARTModelFactoryProtegeImpl;
import it.uniroma2.art.owlart.protegeimpl.models.OWLModelProtegeImpl;
import it.uniroma2.art.owlart.protegeimpl.start.AGROVOC_INFO;
import it.uniroma2.art.owlart.vocabulary.RDFResourceRolesEnum;

import java.util.Iterator;

import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AGROVOC_GENERICSEARCH_ProtegeDB_TestOld {

	protected static Logger logger = LoggerFactory.getLogger(AGROVOC_GENERICSEARCH_ProtegeDB_TestOld.class);

	protected static String dbusername = "root";
	protected static String dbpwd = "mysqlstarred";
	
	public static void main(String[] args) {
		try {

			String chosenDB = AGROVOC_INFO.current_agrovoc_small_DB;
			
			ARTModelFactoryProtegeImpl fact = new ARTModelFactoryProtegeImpl();			
			
			OWLModelProtegeImpl model = (OWLModelProtegeImpl) fact.loadOWLModel("com.mysql.jdbc.Driver",
					"jdbc:mysql://127.0.0.1:3306/" + chosenDB
							+ "?requireSSL=false&useUnicode=true&characterEncoding=UTF-8", chosenDB,
					dbusername, dbpwd);

			String indexLocation = "./data/index";

			StandardAnalysisConfiguration conf = new StandardAnalysisConfiguration(Version.LUCENE_36, "en", "it");
			conf.setLanguagesOfURILocalNames("en", "it");
			OWLSearchEngine oi = new OWLSearchEngine(model, indexLocation, conf);

			System.out.println("building index at: " + indexLocation);
			oi.buildIndex(true, RDFResourceRolesEnum.cls);
			logger.info("INDEX BUILT");

			oi.createOntIndexSearcher();
			System.out.println("ontsearcher armed");

			Iterator<ARTURIResource> retrievedResources;

			// *************************************
			// example over AGROVOC ontology
			// *************************************

			oi.setTHRESHOLD(0.2);

			retrievedResources = oi.findClasses("deer", "en", OntIndexSearcher.FUZZY_MATCH)
					.iterator();

			while (retrievedResources.hasNext())
				System.out.println(retrievedResources.next());

			retrievedResources = oi.findClasses("Concetti di dominio", "it",
					OntIndexSearcher.FUZZY_MATCH).iterator();

			while (retrievedResources.hasNext())
				System.out.println(retrievedResources.next());

			/*
			 * retrievedResources = oi.findProperties("ha rappresentazione", "it",
			 * OntIndexSearcher.FUZZY_MATCH).iterator();
			 * 
			 * while (retrievedResources.hasNext()) System.out.println(retrievedResources.next());
			 */

		} catch (Exception e) {
			e.printStackTrace(System.err);
			logger.error(e.getMessage());

			System.err.flush();
			System.err.println("error");

			e.printStackTrace();
		}
	}

}
